package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeSevice;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.Optional;

/**
 * @author ljj
 * @create 2022-11-14-11:39
 */
@RestController
@RequestMapping("/employee")
@Slf4j
public class EmployeeController {
    @Autowired
    private EmployeeSevice employeeSevice;
    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee){
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        LambdaQueryWrapper<Employee> queryWrapper =new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername,employee.getUsername());
        Employee emp = employeeSevice.getOne(queryWrapper);
        if (emp==null){
            return R.error("登入失败！");
        }
        if (!password.equals(emp.getPassword())){
            return R.error("登入失败！");
        }
        if (emp.getStatus()==0){
            return R.error("账号已禁用！");
        }
        request.getSession().setAttribute("employee",emp.getId());
        return R.success(emp);
    }
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        request.getSession().invalidate();
        return R.success("退出成功");
    }
    @PostMapping()
    public R<String> add(HttpSession session, @RequestBody Employee employee){
        log.info("111");
        employee.setPassword( DigestUtils.md5DigestAsHex("123456".getBytes()));
        /*employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());
        employee.setCreateUser((Long) session.getAttribute("employee"));
        employee.setUpdateUser((Long) session.getAttribute("employee"));*/
        employeeSevice.save(employee);
        return R.success("新增成功");
    }
    @GetMapping("/page")
    public R<Page> getPage(int page,int pageSize,String name){
        log.info("page={},pagesize={},name={}",page,pageSize,name);
        Page<Employee> page1 = new Page(page, pageSize);
        LambdaQueryWrapper<Employee> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.like(StringUtils.isNotBlank(name),Employee::getName,name).orderByDesc(Employee::getUpdateTime);
        Page<Employee> pageInfo = employeeSevice.page(page1, lambdaQueryWrapper);

        return R.success(pageInfo);
    }
    @PutMapping()
    public R<String> update(HttpSession session, @RequestBody Employee employee){
        log.info("111");
        employee.setPassword( DigestUtils.md5DigestAsHex("123456".getBytes()));

        long id = Thread.currentThread().getId();
        log.info("updatecontroller id:{}",id);
        employeeSevice.updateById(employee);
        return R.success("修改成功");
    }
    @GetMapping("/{id}")
    public R<Employee> getByid(HttpSession session, @PathVariable Long id){
        log.info("111");
        log.info("根据id查询员工:{}" ,id);
        Employee emp = employeeSevice.getById(id);
        if (emp !=null){
            return R.success(emp);
        }
        return R.error("没有查询到对应员工");
    }

}
